package com.sap.jcp.sm.email.util.scheduler;

import java.util.ArrayList;
import java.util.List;

import org.quartz.CronScheduleBuilder;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

import com.sap.jcp.sm.email.util.EmailManager;

public class TimeLineScheduler implements Job {
	private static TimeLineScheduler timeLineScheduler = new TimeLineScheduler();
	private boolean scheduleStarted = false;
	Scheduler scheduler;
	private TimeLineScheduler() {
	}
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		//System.out.println("Time  line scheduler started.");
		List toList = new ArrayList<String>();
		toList.add("ssnakaran@sapient.com");
		new EmailManager().sendMessage("Schedule Next week.", "Reminder : Schedule next week.", toList , "jcpsm@sapient.com");
	}
	
	public void triggerScheduler() {
		if (scheduleStarted) {
			return;
		}
		
		try {
			JobDetail job = JobBuilder.newJob(TimeLineScheduler.class)
					.withIdentity("dummyJobName", "group1").build();
	
			Trigger trigger = TriggerBuilder.newTrigger().withIdentity(
				"dummyTriggerName", "group1").withSchedule(
				CronScheduleBuilder.cronSchedule("0 15 10 ? * THU")).build();
			scheduler = new StdSchedulerFactory().getScheduler();
			
			scheduler.start();
			scheduler.scheduleJob(job, trigger);
			scheduleStarted = true;
		} catch (Exception e) {
			//System.out.println("Time  line scheduler : exception.");
			e.printStackTrace();
			try {
				scheduler.shutdown();
			} catch (SchedulerException e1) {
				e1.printStackTrace();
			}
			scheduleStarted = false;
		}
	}
	public static TimeLineScheduler getInstance() {
		return timeLineScheduler;
	}
	
}
